﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Systemet.Technical;

namespace Systemet.Domain
{
    class LoginController
    {
        private LoginConnection lc;
        private frmLogin loginForm;
        private Form1 mainForm;
        private User user;
        private UserRole userRole;

        public LoginController()
        {
            lc = new LoginConnection();
            mainForm = new Form1();
        }

        public void setLoginForm(frmLogin loginForm)
        {
            this.loginForm = loginForm;
        }

        //REGULAR LOGIN
        public bool validateLogin(string anvdID, string pass)
        {
            user = new User();

            user.Password = pass;
            user.UserName = anvdID;

            user = lc.validateLogin(user);

            // > 0 to identify a -1-"problem"
            if (user.RoleID > 0 && user.RoleID != 11)
            {
                userRole = new UserRole();
                userRole = lc.getUserRole(user);

                setUserInfo();
                return true;
            }
            // Identifies "guest user"
            else 
            {
                return false;
            }
        }

        //GUEST LOGIN
        public bool validateLogin()
        {
            user = new User();
            user.Password = "";
            user.UserName = "";
            user.RoleID = 11;

            userRole = new UserRole();
            userRole = lc.getUserRole(user);

            setUserInfo();

            if (AccessController.Instance.LoggedInUser.RoleID == 11)
                return true;
            else
                return false;
        }

        private void setUserInfo()
        {
            AccessController.Instance.LoggedInUser = user;
            AccessController.Instance.UserRoleLoggedInUser = userRole;
        }

        public void changeFormToMain()
        {
            loginForm.Hide();
            mainForm.Show();
        }

        public bool validateTextLength(string tempAnvdID, string tempPass)
        {
            if (tempAnvdID.Length > 3 && tempPass.Length > 3)
                return true;
            else
                return false;
        }

        public void logOut()
        {
            loginForm = new frmLogin();
            loginForm.Show();
        }
    }
}
